package com.xzit.servlet;

import com.xzit.entry.Person;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author haoranlu
 * @project IntelliJ IDEA
 */
public class FristServlet extends HttpServlet {
    /*重写HttpServlet doGet doPost方法，实现根据不同的用户Http请求方式调用相关方法*//*

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行doGet方法");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行doPost方法");
    }*/
    /*重写父类中的service方法*/

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*使用请求对象获取用户提交的请求参数*/
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String strSex = sex.equalsIgnoreCase("m") ? "男" : "女";
//        System.out.println("用户的信息是:\n 姓名:"+name+"性别是:"+strSex);
        Person per = new Person(name,strSex);
        req.setAttribute("person",per);
        /*将per对象存储在HttpRequest 请求作用域中*/
        RequestDispatcher rd = req.getRequestDispatcher("showinfo.jsp");//返回的是发报机对象
        /*使用请求发报机的forward方法实现请求转发页面跳转*/
        rd.forward(req,resp);
    }
}
